package com.zlj.jdbc.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 测试两个线程是否可以同时访问一个连接
 * @author bashen
 *
 */
public class Demo02 implements Runnable{
	
	@Override
	public void run() {
		Connection conn = null;
		PreparedStatement statement = null;
		try{
			conn = SingleJdbcUtils.getInstance().getConnection();
			conn.setAutoCommit(false);
			String sql = "select * from teacher where tid = ?";
			statement = conn.prepareStatement(sql);
			statement.setInt(1, 5);
			ResultSet resultSet = statement.executeQuery();
			if(resultSet != null) {
				while(resultSet.next()) {
					Thread.sleep(5000);
					String name = resultSet.getString("tname");
					System.out.println(name + "=============" + conn);
				}
			}
			conn.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				//if(statement != null) statement.close();
				//if(conn != null) conn.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

    public static void main(String[] args) {
	    Demo02 d = new Demo02();	
		Thread t1 = new Thread(d, "线程1");
		Thread t2 = new Thread(d, "线程2");
		t1.start();
		t2.start();
	}
}
